#! /usr/bin/perl
=head1 NAME

convert.pl - converts old email log format into new.  

=head1 DESCRIPTION

A good example of how perl can be used to do simple conversion.

=head1 DEPENDENCIES

L<MLDBM>, L<MLDBM::Sync>

=cut

require configvars;                  # Found via PERL5LIB env variable
use MLDBM::Sync;                     # this gets the default, SDBM_File
use MLDBM qw( DB_File Storable );    # use Storable for serializing
use Fcntl qw(:DEFAULT);              # import symbols O_CREAT & O_RDWR for use with DBMs
use strict;

=head1 SYNOPSIS

  perl convert.pl

=cut

our ( %database );

unlink $MauchConfig::maildatabase;

open( MYINPUTFILE, "<" . $MauchConfig::maildir . "/logs/stats.log" );
my @lines = <MYINPUTFILE>;
close( MYINPUTFILE );

my $sync = tie %database, 'MLDBM::Sync', $MauchConfig::maildatabase, O_CREAT | O_RDWR, 0640
  or die "Cannot open file $MauchConfig::maildatabase $!\n";

$sync->Lock;
foreach my $line ( @lines ) {
    ( $date, $score, $type, $from, $email, $subject, $folder ) = split( /\t/, $line );

    my $tmp = $database{ $date };
    $tmp->{ 'date' }    = $date;
    $tmp->{ 'score' }   = $score;
    $tmp->{ 'type' }    = $type;
    $tmp->{ 'from' }    = $from;
    $tmp->{ 'email' }   = $email;
    $tmp->{ 'subject' } = $subject;
    $tmp->{ 'folder' }  = $folder;
    $database{ $date }  = $tmp;
} ## end foreach my $line ( @lines )
$sync->UnLock;
untie %database;

=head1 BUGS AND LIMITATIONS

No bugs have been reported.

Please report any bugs or feature requests to C<cmauch@gmail.com>

=head1 AUTHOR

Charles Mauch <cmauch@gmail.com>

=head1 LICENSE

Copyright (c) 2006 Charles Mauch

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.  See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
Street, Fifth Floor, Boston, MA  02110-1301, USA.

=head1 SEE ALSO

perl(1).

=cut

# $Id$
